﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessBingo.Model;
using BusinessBingo.Model.Interfaces;
using BusinessBingo.Queries;
using BusinessBingo.Web.Controllers.Base;
using BusinessBingo.Web.Infrastructure.InfoStores.Interfaces;
using BusinessBingo.Web.Models.ViewModels.Words;
using BusinessBingo.QueryHandlers;
using MvcMiniProfiler;

namespace BusinessBingo.Web.Controllers
{
    public class WordsController : SessionlessBaseController
    {

        /// <summary>
        /// The language store is responsible for setting a specific language.
        /// </summary>
        private ICurrentLanguageStore _languageStore;

        /// <summary>
        /// Ctor for new instances
        /// </summary>
        /// <param name="currentLanguageStore"></param>
        public WordsController(ICurrentLanguageStore currentLanguageStore)
            : base()
        {
            _languageStore = currentLanguageStore;
        }

        [HttpGet]
        [OutputCache(Duration = 600)]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public JsonResult List()
        {
            GetWordsQueryHandler handler = new GetWordsQueryHandler();
            GetWordsQuery query = new GetWordsQuery(LanguageKey.De, 1, 10);
            var result = handler.Execute(query);

            WordListViewModel model = new WordListViewModel();
            model.Words = new List<WordViewModel>();

            foreach (Word word in result.Words)
            {
                WordViewModel wordViewModel = new WordViewModel();
                wordViewModel.Name = word.Value;
                wordViewModel.Upvotes = word.Upvotes;
                wordViewModel.Downvotes = word.Downvotes;

                model.Words.Add(wordViewModel);
            }

            return Json(model, JsonRequestBehavior.AllowGet);            
        }

    }
}
